import bisect
class Solution(object):
    def minAbsoluteSumDiff(self, nums1, nums2):
        base = 10 ** 9 + 7
        n = len(nums1)
        total = sum([abs(nums1[i] - nums2[i]) for i in range(n)])
        if n == 1:
            return total
        new_nums1 = nums1.copy()
        new_nums1.sort()
        max_val = - float('inf')
        for index, num in enumerate(nums2):
            new_index = bisect.bisect_right(new_nums1, num)
            pre = abs(nums2[index] - nums1[index])
            new = float('inf')
            if new_index > 0:
                new = min(new, abs(new_nums1[new_index - 1] - nums2[index]))
            if new_index < n:
                new = min(new, abs(new_nums1[new_index] - nums2[index]))
            max_val = max(pre - new, max_val)

        return (total - max_val) % base

data = Solution()
nums1 = [1,10,4,4,2,7]
nums2 = [9,3,5,1,7,4]
print(data.minAbsoluteSumDiff(nums1, nums2))